package com.hyg.module_report.ui.activity;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hyg.lib_base.mainUtils.StatusBarUtil;
import com.hyg.lib_base.mainUtils.Utils;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.module_report.R;
import com.hyg.module_report.model.HealthCareData;
import com.hyg.module_report.model.ReportIDToHealthCareID;
import com.hyg.module_report.model.ReportListData;
import com.hyg.module_report.ui.fragment.HealthAdviceDetailFragment;
import com.hyg.module_report.ui.fragment.ReportDetailFragment;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * description: 用户报告展示页
 *
 * @author visitant
 * @time 2020/12/3 13:06
 */

public class HealthReportActivity extends BaseActivity {

    public String MedicalId = "";
    private String TongueID = "";
    private String SmellID = "";
    private String PulseID = "";
    private String EarID = "";
    private String BodyID = "";
    private String PhysiqueType = "";
    private String DietInfo = "";
    private String MedicineInfo = "";
    private String TeaInfo = "";
    private String MoxibustionHealth = "";

    private ImageView back;
    private TextView tv_report;
    private TextView tv_advice;
    private TextView reportUser;
    private TextView reportAddr;
    private TextView reportDate;
    private LinearLayout fragmentLN;
    private Fragment currentFragment = new Fragment();

    private ArrayList<HealthCareData> HealthCareData_dataList = new ArrayList<HealthCareData>();

    private ArrayList<HealthCareData> HealthCareData_xiezi = new ArrayList<HealthCareData>();
    private ArrayList<HealthCareData> HealthCareData_yundong = new ArrayList<HealthCareData>();
    //    private ArrayList<SpannableString> HealthCareData_yundongjinji = new ArrayList<SpannableString>();
    private ArrayList<HealthCareData> HealthCareData_yingji = new ArrayList<HealthCareData>();
    private ArrayList<HealthCareData> HealthCareData_shanshi = new ArrayList<HealthCareData>();
    private ArrayList<HealthCareData> HealthCareData_yaoyu = new ArrayList<HealthCareData>();
    private ArrayList<HealthCareData> HealthCareData_aijiu = new ArrayList<HealthCareData>();
    private ArrayList<HealthCareData> HealthCareData_chayin = new ArrayList<HealthCareData>();

    private ReportIDToHealthCareID reportIDToHealthCareID = new ReportIDToHealthCareID();


    private List<ReportIDToHealthCareID> templist;
    private int existence = -1;

    FragmentManager fragmentManager;
    FragmentTransaction transaction;
    ReportDetailFragment reportFragment;
    HealthAdviceDetailFragment healthFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_health_report);
        init();
    }

    /**
     * @description: 初始化
     * @author visitant
     * @time 2020/12/10 9:54
     */
    public void init() {
        initFragment();
        initView();
        initData();
    }

    public void initFragment() {
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        reportFragment = new ReportDetailFragment();
        healthFragment = new HealthAdviceDetailFragment();
        transaction.add(R.id.content_fragment, reportFragment);
        currentFragment = reportFragment;
        transaction.commit();
    }

    /**
     * description: 初始化数据
     *
     * @author visitant
     * @time 2020/12/10 9:54
     */
    public void initData() {
        Intent it = getIntent();
        if (null != it.getParcelableExtra("MedicalId")) {
            ReportListData item = (ReportListData) it.getParcelableExtra("MedicalId");
            MedicalId = item.getReport_id();
            reportIDToHealthCareID.setReportId(MedicalId);
            Log.d("MyDataBaseHelper", MedicalId + ":");
            Date temp = stringToDate(item.getReport_date());
            reportDate.setText(dateToString(temp, "yyyy年MM月dd日"));
            reportUser.setText(item.getReport_parient_name());
            reportAddr.setText(item.getReport_hospital());
            reportAddr.setSelected(true);
        }
    }

    /**
     * description: 初始化页面
     *
     * @author visitant
     * @time 2020/12/10 9:53
     */
    public void initView() {
        StatusBarUtil.setStatusBar(1, this, getResources().getColor(R.color.white), 00, null, null, true);
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    return;
                }
                finish();
                overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
            }
        });
        reportUser = (TextView) findViewById(R.id.reportUser);
        reportAddr = (TextView) findViewById(R.id.reportAddr);
        reportDate = (TextView) findViewById(R.id.reportDate);
        fragmentLN = findViewById(R.id.content_fragment);

        tv_report = (TextView) findViewById(R.id.tv_report);
        tv_advice = (TextView) findViewById(R.id.tv_advice);
        tv_report.setOnClickListener(new View.OnClickListener() {               //体检报告按钮
            @Override
            public void onClick(View view) {
                tv_report.setTextColor(getResources().getColor(R.color.MainThemeColor));
                tv_advice.setTextColor(getResources().getColor(R.color.gray));
                showFragment(reportFragment);
            }
        });
        tv_advice.setOnClickListener(new View.OnClickListener() {               //养生建议按钮
            @Override
            public void onClick(View view) {
                tv_report.setTextColor(getResources().getColor(R.color.gray));
                tv_advice.setTextColor(getResources().getColor(R.color.MainThemeColor));
                showFragment(healthFragment);
            }
        });
    }

    public Date stringToDate(String dateString) {
        //        ParsePosition position = new ParsePosition(0);
        if (null == dateString || dateString.equals("null") || dateString.equals("")) {
            Date date = new Date();
            return date;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //        Date dateValue = simpleDateFormat.parse(dateString, position);
        Date dateValue = null;
        try {
            dateValue = simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            Log.d(CrashTAG, "" + e.getMessage());
        }
        return dateValue;
    }

    public String dateToString(Date data, String formatType) {
        return new SimpleDateFormat(formatType).format(data);
    }


    /**
     * 展示Fragment
     */
    private void showFragment(Fragment fragment) {
        if (currentFragment != fragment) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.hide(currentFragment);
            currentFragment = fragment;
            if (!fragment.isAdded()) {
                transaction.add(R.id.content_fragment, fragment).show(fragment).commit();
            } else {
                transaction.show(fragment).commit();
            }
        }
    }
}

